
var express = require('express');
var router = express.Router();
var process = require('child_process');



var rpio = require('rpio');
var LED_PIN = 29;
rpio.open(LED_PIN, rpio.OUTPUT,rpio.LOW);


// 打开LED
function turnOn() {
	rpio.write(LED_PIN, rpio.HIGH);
}
// 关闭LED
function turnOff() {
	rpio.write(LED_PIN, rpio.LOW);
}




router.all('/raspberry/index', function(req, res, next) {
	var param = req.query || req.params; 
	let status = param.status;
	if (status === '1') {  // 开
		turnOn();
		res.end(JSON.stringify({
			msg: '开灯成功',
			status: 100,
		}));
	} else { // 关
		turnOff();
		res.end(JSON.stringify({
			msg: '关灯成功',
			status: 100,
		}));
	}
})


router.all('/webhook/index', function(req, res, next) {
	process.exec('cd /home/manage && git pull',
	  function (error, stdout, stderr) {
			console.log('exec error: ' + error);
			console.log('exec stdout: ' + error);
			console.log('exec stderr: ' + stderr);
			if (error !== null) {
				console.log('exec error: ' + error);
			}
		});
	res.end(JSON.stringify({
		msg: 'webhook/index',
		status: 100,
		res: 'res'
	}));
})

router.all('/webhook/alsm', function(req, res, next) {
	process.exec('cd /home/alsm && git pull',
	  function (error, stdout, stderr) {
			console.log('exec error: ' + error);
			console.log('exec stdout: ' + error);
			console.log('exec stderr: ' + stderr);
			if (error !== null) {
				console.log('exec error: ' + error);
			}
		});
	res.end(JSON.stringify({
		msg: 'webhook/alsm',
		status: 100,
		res: 'res'
	}));
})


module.exports = router;
